<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibMember;
use app\common\model\hfztcy\LibWithdrawalApply;
use app\common\model\LibUser;
use common\Common;


/**
 * 提现记录
 * @name WithdrawalRecords.php
 * @package hfztcy
 * @category controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class WithdrawalRecords extends AdminController
{
    protected $model;
    protected $lib_user;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibWithdrawalApply();
        $this->lib_user = new LibUser();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 所有-列表页
     */
    function withdrawalAllList()
    {
        $this->getSetMenu($this);
        $this->assign('state','');
        $this->log(__CLASS__, __FUNCTION__, "所有-列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * 游客-列表页
     */
    function withdrawalList()
    {
        $this->getSetMenu($this);
        $this->assign('state',0);
        $this->log(__CLASS__, __FUNCTION__, "游客-列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * 普通会员-列表页
     */
    function withdrawal1List()
    {
        $this->getSetMenu($this);
        $this->assign('state',1);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * vip会员-列表页
     */
    function withdrawal2List()
    {
        $this->getSetMenu($this);
        $this->assign('state',2);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * vip会员-列表页
     */
    function withdrawal3List()
    {
        $this->getSetMenu($this);
        $this->assign('state',5);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * 红娘会员-列表页
     */
    function withdrawal4List()
    {
        $this->getSetMenu($this);
        $this->assign('state',3);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * 门店会员-列表页
     */
    function withdrawal5List()
    {
        $this->getSetMenu($this);
        $this->assign('state',4);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('WithdrawalRecords/withdrawalList/withdrawalList');
    }

    /**
     * 详情页面
     */
    function detailWithdrawal()
    {
        $this->getMenu($this);
        $withdrawalInfo = $this->model->findWithdrawalApply(['id' => input("id")]);
        $withdrawalInfo = $withdrawalInfo['data'];
        $userRes = $this->lib_user->findUser(['id' => $withdrawalInfo['user_id']]);
        $withdrawalInfo['nick_name'] = $userRes['data']['nick_name'];
        $this->assign('withdrawalInfo', $withdrawalInfo);
        $this->log(__CLASS__, __FUNCTION__, "详情页面", 1, 'view');
        return $this->fetch('WithdrawalRecords/detailWithdrawal/detailWithdrawal');
    }

    /**
     * 编辑页面
     */
    function editWithdrawal()
    {
        $this->getMenu($this);
        $withdrawalInfo = $this->model->findWithdrawalApply(['id' => input("id")]);
        $this->assign('withdrawalInfo', $withdrawalInfo['data']);
        $this->log(__CLASS__, __FUNCTION__, "编辑页面", 1, 'view');
        return $this->fetch('WithdrawalRecords/editWithdrawal/editWithdrawal');
    }

    /**
     * 删除
     */
    function deleteWithdrawal()
    {
        $id = input("id");
        $result = $this->model->deleteWithdrawalApply(["id" => $id]);
        echo json_encode($result);
    }

    /**
     * 分页查询
     */
    function pagingWithdrawal()
    {
        $args = $this->getArgsList($this, ['user_id', 'username','state']);
        $page = $this->getPageInfo();
        $where = [];
        if (!empty($args['state'])) {
            if ($args['state'] != 5) {
                $where[] = "FIND_IN_SET({$args['state']},identity)";
            } else {
                $where[] = "(`a`.`is_vip_video` > 0 or `a`.`is_red_mother_video` > 0)";
            }
        }
        if (!empty($args['user_id'])) $where[] = "`user_id` = " . $args['user_id'];
        if (!empty($args['username'])) $where[] = "`memberUsername` = '" . $args['username'] . "'";
        $result = $this->model->pagingWithdrawalApply($page, implode(' AND ', $where));
        echo json_encode($result);
    }

    /**
     * Info: 审核提现申请
     * Date: 2022/6/20 11:24
     */
    public function auditStatus()
    {
        $id = input("id");
        $status = input("state");

        $result = $this->model->findWithdrawalApply(['id' => $id]);
        if ($result['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, '不存在', ''));
            die();
        }
        if ($result['data']['audit_status'] > 0) {
            echo json_encode(Common::errorArray(1, '已审核', ''));
            die();
        }

        $user_id = (int)$result['data']['user_id'];//会员
        $amount = (int)$result['data']['amount'];//提现金额
        $trans_money = (int)$result['data']['trans_money'];//实际拨款金额

        $memberModel = new LibMember();
        $memberInfo = $memberModel->findMember(['user_id' => $user_id]);
        if ($memberInfo['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, '用户不存在', ''));
            die();
        }

        $params = [
            'id' => (int)$id,
            'status' => (int)$status,
            'user_id' => $user_id,
            'amount' => (float)$amount,
            'trans_money' => (float)$trans_money,
            'balance' => (float)$memberInfo['data']['balance']
        ];

        $this->model->withdrawalAudit($params);
    }
}

?>